<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("profileFunction.php");

$errMsg = "";
$description = "";
$note = "";
$description = sxREQUEST("description");
$note = sxREQUEST("note");
$id_profile = sxREQUEST("id_profile");
if(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "save"){
	$errMsg .= profileMod();	
}
if(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "delete"){
	$errMsg .= profileDel();	
}
if(isset($_REQUEST["id"])){
	$id_profile = $_REQUEST["id"];
}
if($id_profile != "" && is_numeric($id_profile)){
	$sSQL = "SELECT * FROM profile WHERE id_profile=$id_profile";
	if($objSDAO->query($sSQL, "sel", array("contOnErr"=>true))){
		if($row = $objSDAO->fetch("sel")){
			$description = $row["description"];
			$note = $row["note"];
			$sSQL = "SELECT * FROM flag_profile WHERE id_profile=$id_profile ORDER BY flag";
			if(!$objSDAO->query($sSQL, "selFlag", array("contOnErr"=>true))){
				$errMsg .= $sSQL." - ".$objSDAO->error;
			}
			$sSQL = "SELECT u.* FROM user u " .
					"INNER JOIN user_profile up ON u.id_user=up.id_user " .
					"WHERE up.id_profile=$id_profile " .
					"ORDER BY u.last_name, u.name";
			if(!$objSDAO->query($sSQL, "selUser", array("contOnErr"=>true))){
				$errMsg .= $sSQL." - ".$objSDAO->error;
			}
		}else{
			$errMsg .= _t("Impossibile caricare i dati del profilo utente");
		}
	}else{
		$errMsg .= $sSQL." - ".$objSDAO->error;
	}
}else{
	$errMsg.= _t("Profilo non definito");
}

//output HTML
$aOpz = array("tit2"=>_t("Aggiorna il profilo"), "isReloadable"=>false);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/ajax.js"></script> 
<script language="JavaScript" type="text/javascript" src="../../_jsc/form.js"></script>  
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		var errMsg = "";
		var objFrm = document.getElementById("objFrm");
		var objMessArea =  document.getElementById("messArea");
		
		var obj = document.getElementById("description");
		if(obj.value == ""){
			errMsg += "<?php echo _t("La description &egrave; obbligatoria");?>.<br>";
			obj.className = "inpt01";
			//obj.focus();
		}else{
			obj.className = "inpt01";
		}
		if(errMsg == ""){
			objFrm.submit();
		}else{
			objMessArea.innerHTML = errMsg;
		}		
	}
	function showHide(ctrl){
		var obj = document.getElementById(ctrl);
		if(obj.className == "hidden")
			obj.className = "show";
		else
			obj.className = "hidden"
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//form inserimento dati
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"profileMod.php\" method=\"POST\">");
echo("<input type=\"hidden\" name=\"xsn\" id=\"xsn\" value=\"".XSN."\">");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">");
echo("<input type=\"hidden\" name=\"id_profile\" id=\"id_profile\" value=\"$id_profile\">");
echo("<input type=\"hidden\" name=\"description\" id=\"description\" value=\"".sxHE($description)."\">");
//output comandi
$aCmd = array("ins"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("conferma modifica dati"),
										"text"=>_t("conferma"),
										"perm"=>"updpro"),
						"back"=>array("pag"=>$objUser->getHistory(0,false),
										"title"=>_t('cancel'),
										"text"=>_t('cancel'))
					);
echo(commandsHTML($aCmd));	
echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan=\"2\">"._t("Profilo")."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("description")."</td>" .
			"<td class=\"text01\">" .
			"<input type='text' name='description' id='description' value=\"".sxHE($description)."\" class='inpt01'></td>" .
		"</tr>\n");
echo("<tr>" .
				"<td class=\"labl01\">"._t("note")."</td>" .
				"<td class=\"text01\"><textarea class=\"inpt01\" name=\"note\" id=\"note\" cols=\"50\" rows=\"8\">".sxHE($note)."</textarea></td>" .
			"</tr>\n");
echo("</table>");

echo(commandsHTML($aCmd));
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
$objSDAO->close();
?>
